﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using Informator.Properties;
using System.Text.RegularExpressions;


namespace Informator.Presentation.Vesti
{
    /// <summary>
    /// Interaction logic for VestAkt.xaml
    /// </summary>
    public partial class VestAkt : UserControl
    { 
            
      public  List<Vesti> listaVesti = new List<Vesti>();


        public VestAkt()
        {
           
           
            InitializeComponent();
            VestAkt.ListaVesti(listaVesti);
           

        }

        public static void ListaVesti(List<Vesti> listaVesti)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(@"http://www.viser.edu.rs/xml/rss-vesti.php");

            XmlNodeList nodes = doc.DocumentElement.SelectNodes("channel/item");

            foreach (XmlNode node in nodes)
            {
                Vesti vest = new Vesti();
                
                vest.naslov = node.SelectSingleNode("title").InnerText;
                vest.vest_detalji = node.SelectSingleNode("description").InnerText;
                vest.datum = DateTime.Parse(node.SelectSingleNode("pubDate").InnerText);
                vest.link = node.SelectSingleNode("link").InnerText;
                
               int index = vest.naslov.IndexOf("(");

               if (index > 0)
               { 
                   vest.vest_predmet = vest.naslov.Substring(index + 1, (vest.naslov.Length - index) - 2);
                    vest.naslov = vest.naslov.Substring(0,  index );
                   
               }
               vest.vest_tekst = vest.vest_detalji;
               vest.vest_tekst = IzbrisiHtml(vest.vest_tekst);
                vest.vest_detalji = vest.vest_detalji.Substring(0,80);
                vest.vest_detalji += "....";
                vest.vest_detalji = IzbrisiHtml(vest.vest_detalji);
                vest.br++; 
                
               
            listaVesti.Add(vest);
            }
           
        }
        public static string IzbrisiHtml(string source)
        {
            return Regex.Replace(source, @"<[^>]+>|&nbsp;", string.Empty);
        }
    }
}
